package com.dgut.controller;

import com.dgut.domain.Customer;
import com.dgut.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 获得客户列表
     * @param pageCur
     * @return
     */
    @RequestMapping("GetCustomerList")
    public ModelAndView GetCustomerList(Integer pageCur){
        ModelAndView modelAndView = new ModelAndView();
        int temp = customerService.customerNum();
        System.out.println("客户总数："+temp);
        modelAndView.addObject("totalCount",temp);
        int totalPage = 0;
        if (temp == 0) {
            totalPage = 0;//总页数
        } else {
            //返回大于或者等于指定表达式的最小整数
            totalPage = (int) Math.ceil((double) temp / 5);
        }
        if (pageCur == null) {
            pageCur = 1;
        }
        if ((pageCur - 1) * 5 > temp) {
            pageCur = pageCur - 1;
        }
        //分页查询
        Map<String, Object> map = new HashMap<String, Object>();
        int startIndex = (pageCur - 1) * 5;
        int perPageSize = 5;
        /*
         * map.put("startIndex", (pageCur - 1) * 10);//起始位置 map.put("perPageSize",
         * 10);//每页10个
         */
        List<Customer> customers = customerService.findAllByPage(startIndex,perPageSize);
        modelAndView.addObject("totalPage",totalPage);
        modelAndView.addObject("pageCur",pageCur);
        modelAndView.addObject("customers",customers);
        modelAndView.setViewName("main");
        return modelAndView;
    }

    /**
     * 根据id删除客户信息
     * @param id
     * @return
     */
    @RequestMapping("deleteCustomer")
    public String deleteCustomer(Integer id){
        System.out.println("删除"+id);
        customerService.deleteCustomer(id);
        return "redirect:GetCustomerList?pageCur=1";
    }

    /**
     * 插入客户
     * @param customer
     * @return
     */
    @RequestMapping("insertCustomer")

    public String insertCustomer(Customer customer, HttpServletRequest request){
        System.out.println("添加用户信息："+customer);
//        if(customer.getName().length() > 20){
//            request.setAttribute("inCmsg_name","姓名不能超过10个字符");
//            return "";
//        }
        customerService.insertCustomer(customer);
        return "redirect:GetCustomerList?pageCur=1";
    }

    /**
     * 通过id获得客户信息
     * @param id
     * @return
     */
    @RequestMapping("getCustomerById")
    public ModelAndView getCustomerById(int id){
        Customer customer = customerService.getCustomerById(id);
        System.out.println("修改前客户信息"+customer);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customer",customer);
        modelAndView.setViewName("updateCustomer");
        return modelAndView;
    }

    /**
     * 更新客户信息
     * @param customer
     * @return
     */
    @RequestMapping("updateCustomer")
    public ModelAndView updateCustomer(Customer customer){
        System.out.println("修改后客户信息："+ customer);
//        修改
        customerService.updateCustomer(customer);
        ModelAndView modelAndView = new ModelAndView();
//        pagecur参数待优化
        modelAndView.addObject("pageCur",1);
        modelAndView.setViewName("redirect:GetCustomerList");
        return modelAndView;
    }

    @RequestMapping("selectCustomerByParam")
    public ModelAndView selectCustomerByPara(Customer customer){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("查询信息："+customer);
        List<Customer> customers = customerService.selectCustomerByPara(customer);
        System.out.println("查询结果："+customers);
        modelAndView.addObject("customers",customers);
        modelAndView.addObject("ifselect","yes");
        modelAndView.setViewName("main");
        return modelAndView;
    }

//    @RequestMapping("testTran")
//    @Transactional
//    public  ModelAndView TestTran(){
//        customerService.in
//    }
}
